﻿using EONFULLSIN2.Services.DB;
using EONFULLSIN2.Services.Message;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static EONFULLSIN2.Events.MessageEvent;

namespace EONFULLSIN2.ViewModelBase
{
    /// <summary>
    /// 导航的ViewModel
    /// </summary>
    public class NavigationViewModel : BaseViewModel, IConfirmNavigationRequest
    {
        private IRegionNavigationJournal _journal;

        protected readonly IRegionManager _regionManager;
        protected readonly IDialogService _dialog;
        protected readonly IEventAggregator _eventAggregator;
        protected readonly IMessageService _message;
        protected readonly IDbOperate<IFreeSqlMainContext> _mainDbOperate;

        protected NavigationParameters _parameters;

        /// <summary>
        /// 连接日志事件
        /// </summary>
        protected readonly ConnectLogEvent _connectLogUIEvent;
        /// <summary>
        /// 运行日志事件
        /// </summary>
        protected readonly RunLogEvent _runLogUIEvent;

        public NavigationViewModel(
            IRegionManager regionManager, 
            IDialogService dialog, 
            IMessageService messageBox, 
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate)
        {
            this._regionManager = regionManager;
            this._dialog = dialog;
            this._eventAggregator = eventAggregator;
            this._message = messageBox;
            this._mainDbOperate = mainDbOperate;

            _connectLogUIEvent = _eventAggregator.GetEvent<ConnectLogEvent>();
            _runLogUIEvent = _eventAggregator.GetEvent<RunLogEvent>();
        }

        #region 命令


        /// <summary>
        /// 返回上一页命令
        /// </summary>
        public virtual DelegateCommand GeBackCommand => new DelegateCommand(() => this.GoBack());

        /// <summary>
        /// 返回后一页命令
        /// </summary>
        public virtual DelegateCommand GoForwardCommand => new DelegateCommand(() => this.GoForward());

        #endregion


        /// <summary>
        /// 导航到首页
        /// </summary>
        protected virtual void NavigationHome()
        {
            //_regionManager.RequestNavigate(PrismManager.MainViewRegionName, nameof(MainView), NavigationCallback);
        }

        /// <summary>
        /// 导航到目标页
        /// </summary>
        protected virtual void NavigationTarget(string regionName, string targetName)
        {
            _regionManager.RequestNavigate(regionName, targetName, NavigationCallback);
        }

        /// <summary>
        /// 导航到目标页带参数
        /// </summary>
        protected virtual void NavigationTarget(string regionName, string targetName, NavigationParameters parameters)
        {
            _regionManager.RequestNavigate(regionName, targetName, NavigationCallback, parameters);
        }

        /// <summary>
        /// 导航到目标页带参数和回调
        /// </summary>
        protected virtual void NavigationTarget(string regionName, string targetName, NavigationParameters parameters, Action<NavigationResult> navigationCallback)
        {
            _regionManager.RequestNavigate(PrismRegionManager.MainViewRegionName, targetName, navigationCallback, parameters);
        }

        /// <summary>
        /// 导航回调
        /// </summary>
        protected void NavigationCallback(NavigationResult result)
        {
        }

        /// <summary>
        /// 导航完成前, 此处可以传递过来的参数以及是否允许导航等动作的控制。
        /// </summary>
        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {
            _journal = navigationContext.NavigationService.Journal;
            _parameters = navigationContext.Parameters;
        }

        /// <summary>
        /// 调用以确定此实例是否可以处理导航请求。否则新建实例,true不新建，false新建
        /// </summary>
        public virtual bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        /// <summary>
        /// 当导航离开当前页时, 类似打开A, 再打开B时, 该方法被触发。
        /// </summary>
        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        /// <summary>
        /// 该值觉得是否拦截该导航请求
        /// </summary>
        public virtual void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            bool result = true;
            if (_journal != null)
            {
                if (_journal.CurrentEntry.Uri == navigationContext.Uri)
                {
                    result = false;
                }
                else
                {
                    result = true;
                }
            }
            continuationCallback(result);
        }

        // 返回上一页
        protected void GoBack()
        {
            if (_journal != null)
                _journal.GoBack();
        }

        // 返回后一页
        protected void GoForward()
        {
            if (_journal != null)
                _journal.GoForward();
        }

    }
}
